package com.bj58.ecdata.dashboard.controllers;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.ServletRequestUtils;

import com.bj58.ecdata.dashboard.constant.IncomePlatConst;
import com.bj58.ecdata.dashboard.databll.IncomeNetflowBLL;
import com.bj58.ecdata.dashboard.utils.MyServletRequestUtils;
import com.bj58.wf.mvc.ActionResult;
import com.bj58.wf.mvc.MvcController;
import com.bj58.wf.mvc.annotation.POST;
import com.bj58.wf.mvc.annotation.Path;

/**
 * 收入看板流量模块
 * @author zhaoxiang
 */
@Path("/income/ajax/netflow")
public class IncomeNetflowAjaxController extends MvcController {

	/** 城市-产品类型[精准，置顶，智能]数据
	 * @return
	 */
	@Path("/cityProduct")
	@POST
	public ActionResult getCityProData() {
		try {
			HttpServletRequest request = beat.getRequest();
			String dateGroup = MyServletRequestUtils.getStringParameter(request, "dateGroup", "stat_date");
			String stat_type = MyServletRequestUtils.getStringParameter(request, "statType", "day");
			String businessName = MyServletRequestUtils.getStringParameter(request, "businessName","");
			String cityName = MyServletRequestUtils.getStringParameter(request, "cityName", "北京");
			String index = MyServletRequestUtils.getStringParameter(request, "index", "cost");
			String dateVal = MyServletRequestUtils.getStringParameter(request, "dateVal", "2014-01-01");
			
			String dataType = MyServletRequestUtils.getStringParameter(request, "dataType", "chart");
			
			String data="";
			if(StringUtils.equalsIgnoreCase(dataType, "chart")){
				data = IncomeNetflowBLL.getCityProJson(dateGroup,stat_type,index,businessName,cityName);
			}else if(StringUtils.equalsIgnoreCase(dataType, "table")){
				data = IncomeNetflowBLL.getCityProTableJson(dateGroup,stat_type,businessName,dateVal);
			}
//			log.info(businessName+"--"+cityName+"\n"+data);
			return ViewResult.jsonView(data);
		} catch (Exception e) {
			e.printStackTrace();
			return ViewResult.view("{}");
		}
	}


	/**
	 * 类别-产品类型[精准，置顶，智能]数据
	 * @return
	 */
	@Path("/cateProduct")
	@POST
	public ActionResult getCateProData() {
		try {
			HttpServletRequest request = beat.getRequest();
			String dateGroup = MyServletRequestUtils.getStringParameter(request, "dateGroup", "stat_date");
			String stat_type = MyServletRequestUtils.getStringParameter(request, "statType", "day");
			String businessName = MyServletRequestUtils.getStringParameter(request, "businessName","");
			String cateName = MyServletRequestUtils.getStringParameter(request, "cateName", "北京");
			String index = MyServletRequestUtils.getStringParameter(request, "index", "cost");
			String dateVal = MyServletRequestUtils.getStringParameter(request, "dateVal", "2014-01-01");
			
			String dataType = MyServletRequestUtils.getStringParameter(request, "dataType", "chart");
			
			String data="";
			if(StringUtils.equalsIgnoreCase(dataType, "chart")){
				data = IncomeNetflowBLL.getCateProJson(dateGroup,stat_type,index,businessName,cateName);
			}else if(StringUtils.equalsIgnoreCase(dataType, "table")){
				data = IncomeNetflowBLL.getCateProTableJson(dateGroup,stat_type,businessName,dateVal);
			}
//			log.info(businessName+"--"+cateName+"\n"+data);
			return ViewResult.jsonView(data);
		} catch (Exception e) {
			return ViewResult.view("{}");
		}
	}
	
	
	/**
	 * 获取某个产品类型精准/置顶/智能的 两个城市对比数据
	 * @return
	 */
	@Path("/cityCmp")
	@POST
	public ActionResult getCityCmpData() {
		try {
			HttpServletRequest request = beat.getRequest();
			String dateGroup = MyServletRequestUtils.getStringParameter(request, "dateGroup", "stat_date");
			String stat_type = MyServletRequestUtils.getStringParameter(request, "statType", "day");
			String businessName = MyServletRequestUtils.getStringParameter(request, "businessName","");
			//产品类型，精准/置顶/智能
			String proType = MyServletRequestUtils.getStringParameter(request, "proType","jingzhun");
			String index = MyServletRequestUtils.getStringParameter(request, "index", "cost");
			
			String cityName1 = MyServletRequestUtils.getStringParameter(request, "city1","北京");
			String cityName2 = MyServletRequestUtils.getStringParameter(request, "city2","天津");

//			EnumCate cateEnum = EnumCate.valueOf(businessName);
			String data = IncomeNetflowBLL.getCityCmpJson(dateGroup,stat_type,index,businessName,proType,StringUtils.strip(cityName1),StringUtils.strip(cityName2));
//			log.info(businessName+"--"+cityName1+"-"+cityName2+"--"+proType+"\n"+data);
			return ViewResult.jsonView(data);
		} catch (Exception e) {
			e.printStackTrace();
			return ViewResult.view("{}");
		}
	}
	
	
	/**
	 * 获取某个产品类型精准/置顶/智能的 两个类别对比数据
	 * @return
	 */
	@Path("/cateCmp")
	@POST
	public ActionResult getCateCmpData() {
		try {
			HttpServletRequest request = beat.getRequest();
			String dateGroup = MyServletRequestUtils.getStringParameter(request, "dateGroup", "stat_date");
			String stat_type = MyServletRequestUtils.getStringParameter(request, "statType", "day");
			String businessName = MyServletRequestUtils.getStringParameter(request, "businessName","");
			//产品类型，精准/置顶/智能
			String proType = MyServletRequestUtils.getStringParameter(request, "proType","jingzhun");
			String index = MyServletRequestUtils.getStringParameter(request, "index", "cost");
			
			String cateName1 = MyServletRequestUtils.getStringParameter(request, "cate1","");
			String cateName2 = MyServletRequestUtils.getStringParameter(request, "cate2","");

//			EnumCate cateEnum = EnumCate.valueOf(businessName);
			String data = IncomeNetflowBLL.getCateCmpJson(dateGroup,stat_type,index,businessName,proType,StringUtils.strip(cateName1),StringUtils.strip(cateName2));
//			log.info(businessName+"--"+cateName1+"-"+cateName2+"--"+proType+"\n"+data);
			return ViewResult.jsonView(data);
		} catch (Exception e) {
			e.printStackTrace();
			return ViewResult.view("{}");
		}
	}
	
	
	
	/**
	 * 获取收入流量来源  “饼图一级来源”
	 * @return
	 */
	@Path("/sourcePie")
	@POST
	public ActionResult sourcePie() {
		try {
			HttpServletRequest request = beat.getRequest();
			String beginDate = MyServletRequestUtils.getStringParameter(request, "beginDate", "2015-01-01");
			String endDate = MyServletRequestUtils.getStringParameter(request, "endDate", "2015-01-01");
			
			String businessName = MyServletRequestUtils.getStringParameter(request, "businessName","zhaopin");
			String platform = MyServletRequestUtils.getStringParameter(request, "platform", "PC");
			IncomePlatConst plat = IncomePlatConst.valueOf(platform);
			
			String data = IncomeNetflowBLL.getSourcePieDataJson(businessName, plat, beginDate, endDate);
//			log.info(businessName+"--"+platform+"---\n"+data);
			return ViewResult.jsonView(data);
		} catch (Exception e) {
			e.printStackTrace();
			return ViewResult.view("{}");
		}
	}
	
	/**
	 * 获取收入流量来源  “饼图一级来源”
	 * @return
	 */
	@Path("/source1Pie")
	@POST
	public ActionResult source1Pie() {
		try {
			HttpServletRequest request = beat.getRequest();
			String beginDate = MyServletRequestUtils.getStringParameter(request, "beginDate", "2015-01-01");
			String endDate = MyServletRequestUtils.getStringParameter(request, "endDate", "2015-01-01");
			
			String businessName = MyServletRequestUtils.getStringParameter(request, "businessName","zhaopin");
			String platform = MyServletRequestUtils.getStringParameter(request, "platform", "PC");
			String source = MyServletRequestUtils.getStringParameter(request, "source", "站内");
			IncomePlatConst plat = IncomePlatConst.valueOf(platform);
			
			String data = IncomeNetflowBLL.getSource1PieDataJson(businessName, source, plat, beginDate, endDate);
//			log.info(businessName+"--"+source+"---\n"+data);
			return ViewResult.jsonView(data);
		} catch (Exception e) {
			e.printStackTrace();
			return ViewResult.view("{}");
		}
	}
	
	
	/**
	 * 获取收入流量来源  “饼图2级来源”
	 * @return
	 */
	@Path("/source2Pie")
	@POST
	public ActionResult source2Pie() {
		try {
			HttpServletRequest request = beat.getRequest();
			String beginDate = MyServletRequestUtils.getStringParameter(request, "beginDate", "2015-01-01");
			String endDate = MyServletRequestUtils.getStringParameter(request, "endDate", "2015-01-01");
			
			String businessName = MyServletRequestUtils.getStringParameter(request, "businessName","zhaopin");
			String platform = MyServletRequestUtils.getStringParameter(request, "platform", "PC");
			String source1 = MyServletRequestUtils.getStringParameter(request, "source1", "");
			IncomePlatConst plat = IncomePlatConst.valueOf(platform);
			
			String data = IncomeNetflowBLL.getSource2PieDataJson(businessName, source1, plat, beginDate, endDate);
//			log.info(businessName+"--"+source1+"---\n"+data);
			return ViewResult.jsonView(data);
		} catch (Exception e) {
			e.printStackTrace();
			return ViewResult.view("{}");
		}
	}
	
	
	/**
	 * 获取收入流量来源  “曲线图0级来源”
	 * @return
	 */
	@Path("/sourceLine")
	@POST
	public ActionResult sourceLine() {
		try {
			HttpServletRequest request = beat.getRequest();
			
			String businessName = MyServletRequestUtils.getStringParameter(request, "businessName","zhaopin");
			String dateGroup = MyServletRequestUtils.getStringParameter(request, "dateGroup", "stat_date");
			String stat_type = MyServletRequestUtils.getStringParameter(request, "statType", "day");
			String platform = MyServletRequestUtils.getStringParameter(request, "platform", "PC");
			String searchIndex = MyServletRequestUtils.getStringParameter(request, "index", "cost");
			String source1s = ServletRequestUtils.getStringParameter(request, "sources", "");
			IncomePlatConst plat = IncomePlatConst.valueOf(platform);
			
			String dataType = MyServletRequestUtils.getStringParameter(request, "dataType", "chart");
			
			String data="";
			if(StringUtils.equalsIgnoreCase(dataType, "chart")){
				data = IncomeNetflowBLL.getSourceLineDataJson(businessName, dateGroup, stat_type, source1s, plat, searchIndex);
			}else if(StringUtils.equalsIgnoreCase(dataType, "table")){
				String dateVal = MyServletRequestUtils.getStringParameter(request, "dateVal", "2015-01-01");
				data = IncomeNetflowBLL.getSourceLineTableJson(businessName, dateGroup, stat_type,dateVal, source1s, plat);
			}
//			log.info(businessName+"---"+source1s+"--"+searchIndex+"\n"+data);
			return ViewResult.jsonView(data);
		} catch (Exception e) {
			e.printStackTrace();
			return ViewResult.view("{}");
		}
	}
	
	/**
	 * 获取收入流量来源  “曲线图一级来源”
	 * @return
	 */
	@Path("/source1Line")
	@POST
	public ActionResult source1Line() {
		try {
			HttpServletRequest request = beat.getRequest();
			
			String businessName = MyServletRequestUtils.getStringParameter(request, "businessName","zhaopin");
			String dateGroup = MyServletRequestUtils.getStringParameter(request, "dateGroup", "stat_date");
			String stat_type = MyServletRequestUtils.getStringParameter(request, "statType", "day");
			String platform = MyServletRequestUtils.getStringParameter(request, "platform", "PC");
			String searchIndex = MyServletRequestUtils.getStringParameter(request, "index", "cost");
			String source = MyServletRequestUtils.getStringParameter(request, "source", "站内");
			String source1s = ServletRequestUtils.getStringParameter(request, "source1s", "");
			IncomePlatConst plat = IncomePlatConst.valueOf(platform);
			
			String dataType = MyServletRequestUtils.getStringParameter(request, "dataType", "chart");
			
			String data="";
			if(StringUtils.equalsIgnoreCase(dataType, "chart")){
				data = IncomeNetflowBLL.getSource1LineDataJson(businessName, dateGroup, stat_type, source, source1s, plat, searchIndex);
			}else if(StringUtils.equalsIgnoreCase(dataType, "table")){
				String dateVal = MyServletRequestUtils.getStringParameter(request, "dateVal", "2015-01-01");
				data = IncomeNetflowBLL.getSource1LineTableJson(businessName, dateGroup, stat_type,dateVal, source, source1s, plat);
			}
//			log.info(businessName+"--"+source+"-"+source1s+"--"+searchIndex+"\n"+data);
			return ViewResult.jsonView(data);
		} catch (Exception e) {
			e.printStackTrace();
			return ViewResult.view("{}");
		}
	}
	
	/**
	 * 获取收入流量来源  “曲线图二级来源”
	 * @return
	 */
	@Path("/source2Line")
	@POST
	public ActionResult source2Line() {
		try {
			HttpServletRequest request = beat.getRequest();
			
			String businessName = MyServletRequestUtils.getStringParameter(request, "businessName","zhaopin");
			String dateGroup = MyServletRequestUtils.getStringParameter(request, "dateGroup", "stat_date");
			String stat_type = MyServletRequestUtils.getStringParameter(request, "statType", "day");
			String platform = MyServletRequestUtils.getStringParameter(request, "platform", "PC");
			String searchIndex = MyServletRequestUtils.getStringParameter(request, "index", "cost");
			String source1 = MyServletRequestUtils.getStringParameter(request, "source1", "站内");
			String source2s = ServletRequestUtils.getStringParameter(request, "source2s", "");
			IncomePlatConst plat = IncomePlatConst.valueOf(platform);
			
			String dataType = MyServletRequestUtils.getStringParameter(request, "dataType", "chart");
			
			String data="";
			if(StringUtils.equalsIgnoreCase(dataType, "chart")){
				data = IncomeNetflowBLL.getSource2LineDataJson(businessName, dateGroup, stat_type, source1, source2s, plat, searchIndex);
			}else if(StringUtils.equalsIgnoreCase(dataType, "table")){
				String dateVal = MyServletRequestUtils.getStringParameter(request, "dateVal", "2015-01-01");
				data = IncomeNetflowBLL.getSource2LineTableJson(businessName, dateGroup, stat_type,dateVal, source1, source2s, plat);
			}
//			log.info(businessName+"--"+source1+"-"+source2s+"--"+searchIndex+"\n"+data);
			return ViewResult.jsonView(data);
		} catch (Exception e) {
			e.printStackTrace();
			return ViewResult.view("{}");
		}
	}
	
}
